 package com.oanda.blackberry.view;
 
 import com.oanda.blackberry.net.RestException;
 import com.oanda.blackberry.ui.BitmapButtonField;
 import com.oanda.blackberry.ui.ColoredLabelField;
 import com.oanda.blackberry.ui.OandaMainScreen;
 import com.oanda.blackberry.ui.SpacerField;
 import com.oanda.blackberry.ui.SpacerManager;
 import com.oanda.blackberry.util.AbstractViewPanel;
 import com.oanda.blackberry.util.DefaultController;
 import com.oanda.blackberry.util.PropertyChangeEvent;
 import com.oanda.blackberry.util.log.Logger;
 import net.rim.device.api.system.Application;
 import net.rim.device.api.system.Bitmap;
 import net.rim.device.api.ui.Field;
 import net.rim.device.api.ui.FieldChangeListener;
 import net.rim.device.api.ui.Font;
 import net.rim.device.api.ui.Graphics;
 import net.rim.device.api.ui.Manager;
 import net.rim.device.api.ui.MenuItem;
 import net.rim.device.api.ui.Screen;
 import net.rim.device.api.ui.UiApplication;
 import net.rim.device.api.ui.component.BasicEditField;
 import net.rim.device.api.ui.component.BitmapField;
 import net.rim.device.api.ui.component.ButtonField;
 import net.rim.device.api.ui.component.EditField;
 import net.rim.device.api.ui.component.Menu;
 import net.rim.device.api.ui.component.SeparatorField;
 import net.rim.device.api.ui.container.HorizontalFieldManager;
 import net.rim.device.api.ui.container.MainScreen;
 import net.rim.device.api.ui.decor.Background;
 import net.rim.device.api.ui.decor.BackgroundFactory;
 
 public class OtpView extends OandaMainScreen
   implements AbstractViewPanel
 {
   DefaultController controller;
   ColoredLabelField otpExplanationLabel;
   ColoredLabelField helpLabel;
   ColoredLabelField smsSentLabel;
   EditField otpField;
   private String sessionToken;
   private String userName;
   BitmapButtonField submit;
   ButtonField sendOtpButton;
   HorizontalFieldManager row3;
   static final String ERROR_OTP_WRONG = "The one time password you entered is wrong";
   static final String ERROR_COULD_NOT_SEND_SMS = "Could not send SMS, please try again";
   static final String ERROR_PLEASE_ENTER_OTP = "Please enter the one time password you received";
   static final String OTP_EXPLANATION = "Click \"Send\" to obtain a one time password via SMS, than submit below to login.";
   static final String CLICK_HERE_FOR_HELP = "Having trouble logging in?";
   static final String SMS_SENT_LABEL = "SMS sent. The password will expire in 5 minutes. You can retry after 45 seconds";
   MenuItem submitMenu = new MenuItem("Submit", 10, 1000)
   {
     public void run() {
       OtpView.this.login();
     }
   };
 
   MenuItem helpMenu = new MenuItem("Help", 10, 1000)
   {
     public void run() {
       OtpView.this.help();
     }
   };
 
   MenuItem back = new MenuItem("Back", 10, 1001)
   {
     public void run() {
       OtpView.this.onClose();
     }
   };
 
   public OtpView(DefaultController controller)
   {
     this.controller = controller;
     init();
   }
 
   public void modelPropertyChange(PropertyChangeEvent evt) {
     if ((evt.getPropertyName() != null) && (evt.getPropertyName().equals("setErrorMessage")))
     {
       if ((evt.getNewValue() != null) && ((evt.getNewValue() instanceof String))) {
         String statusText = (String)evt.getNewValue();
         diplayError(statusText, 3000);
       }
     }
     else if ((evt.getPropertyName() != null) && (evt.getPropertyName().equals("LoginException"))) {
       if ((evt.getNewValue() != null) && ((evt.getNewValue() instanceof RestException))) {
         RestException ex = (RestException)evt.getNewValue();
         if ((ex != null) && (ex.getErrorMessage().toLowerCase().indexOf("1: access denied") > -1))
         {
           diplayError("The one time password you entered is wrong", 5000);
         }
       }
     }
     else if ((evt.getPropertyName() != null) && (evt.getPropertyName().equals("OtpException"))) {
       if ((evt.getNewValue() != null) && ((evt.getNewValue() instanceof RestException))) {
         RestException ex = (RestException)evt.getNewValue();
         if ((ex != null) && (ex.getErrorMessage().toLowerCase().indexOf("1: access denied") > -1)) {
           setStatusField("");
           removeStatusField();
           diplayError("Could not send SMS, please try again", 5000);
         }
       }
     }
     else if ((evt.getPropertyName() != null) && (evt.getPropertyName().equals("setOtpStatus"))) {
       Logger.log("OtpView setOtpStatus: " + (String)evt.getNewValue());
       if ((evt.getNewValue() != null) && ((evt.getNewValue() instanceof String))) {
         String statusText = (String)evt.getNewValue();
         if ((statusText != null) && (statusText.length() > 0)) {
           setStatusField(statusText);
         }
         else {
           setStatusField("");
           removeStatusField();
         }
       }
     }
     else if ((evt != null) && (evt.getPropertyName().equals("showThrobber"))) {
       showThrobber();
     }
     else if ((evt != null) && (evt.getPropertyName().equals("hideThrobber"))) {
       hideThrobber();
     }
     else if ((evt != null) && (evt.getPropertyName().equals("showOtpView"))) {
       this.controller.pushScreen(this);
     }
     else if ((evt != null) && (evt.getPropertyName().equals("hideOtpView"))) {
       this.controller.popScreen(this);
     }
     else if ((evt != null) && (evt.getPropertyName().equals("sessionTokenForOtp"))) {
       this.sessionToken = ((String)evt.getNewValue());
     }
     else if ((evt != null) && (evt.getPropertyName().equals("userName"))) {
       this.userName = ((String)evt.getNewValue());
     }
   }
 
   protected void makeMenu(Menu menu, int instance) {
     menu.add(this.submitMenu);
     menu.add(this.helpMenu);
     menu.add(this.back);
   }
 
   private void init() {
     getTitleManager().deleteAll();
     getMainManager().setBackground(BackgroundFactory.createSolidBackground(4473924));
 
     initFields();
     setFieldsBg();
   }
 
   private void setFieldsBg() {
     Background solidBackground = BackgroundFactory.createLinearGradientBackground(16777215, 14540253, 16777215, 14540253);
 
     Background greyBg = BackgroundFactory.createSolidBackground(OandaMainScreen.BG_COLOR);
     setBackground(greyBg);
 
     Bitmap header = Bitmap.getBitmapResource("header-480x97.png");
     BitmapField headerBitmapField = new BitmapField(header);
     add(headerBitmapField);
 
     getMainManager().setBackground(greyBg);
     this.otpField.setBackground(solidBackground);
 
     add(new SeparatorField());
 
     add(new SpacerField(300, 10));
 
     HorizontalFieldManager row1 = new HorizontalFieldManager(1152921504606846976L);
     this.otpExplanationLabel.setPadding(10, 30, 30, 30);
     row1.add(this.otpExplanationLabel);
     add(row1);
 
     this.row3 = new HorizontalFieldManager(1152921504606846976L);
     this.row3.add(new SpacerField(30, 1));
     this.row3.add(this.sendOtpButton);
 
     add(this.row3);
 
     add(new SpacerField(300, 10));
 
     add(new SeparatorField());
     add(new SpacerField(300, 10));
 
     HorizontalFieldManager row4 = new HorizontalFieldManager(1152921504606846976L);
     row4.add(new SpacerField(30, 1));
     SpacerManager otpSpacerManager = new SpacerManager(183, 50);
     otpSpacerManager.add(this.otpField);
     row4.add(otpSpacerManager);
     row4.add(new SpacerField(30, 1));
 
     add(row4);
     add(new SpacerField(300, 10));
 
     HorizontalFieldManager row5 = new HorizontalFieldManager(1152921504606846976L);
 
     row5.add(new SpacerField(30, 1));
     row5.add(this.submit);
     add(row5);
 
     add(new SpacerField(300, 10));
     add(new SeparatorField());
     add(new SpacerField(300, 10));
 
     HorizontalFieldManager row6 = new HorizontalFieldManager(1152921504606846976L);
 
     row6.add(new SpacerField(30, 1));
     this.helpLabel.setPadding(0, 10, 10, 10);
     row6.add(this.helpLabel);
     add(row6);
 
     this.sendOtpButton.setChangeListener(new FieldChangeListener()
     {
       public void fieldChanged(Field field, int context) {
         OtpView.this.sendOtp();
       }
     });
     this.submit.setChangeListener(new FieldChangeListener()
     {
       public void fieldChanged(Field field, int context) {
         OtpView.this.login();
       } } );
   }
 
   private void sendOtp() {
     this.controller.sendOtp(this.userName, this.sessionToken);
 
     this.row3.delete(this.sendOtpButton);
     this.row3.insert(this.smsSentLabel, 1);
 
     UiApplication.getUiApplication().invokeLater(new Runnable() {
       public void run() {
         OtpView.this.row3.delete(OtpView.this.smsSentLabel);
         OtpView.this.row3.insert(OtpView.this.sendOtpButton, 1);
       }
     }
     , 45000L, false);
   }
 
   private void help()
   {
     this.controller.displayOtpHelp();
   }
 
   private void initFields()
   {
     this.otpExplanationLabel = new ColoredLabelField(16777215, "Click \"Send\" to obtain a one time password via SMS, than submit below to login.");
 
     this.smsSentLabel = new ColoredLabelField(16777215, "SMS sent. The password will expire in 5 minutes. You can retry after 45 seconds", 18014398509481984L);
     this.smsSentLabel.setPadding(10, 30, 10, 0);
 
     this.otpField = new EditField("", "", 40, 83886080L)
     {
       public boolean isFocusable() {
         return true;
       }
 
       public void paint(Graphics g) {
         g.setColor(3355443);
         super.paint(g);
       }
     };
     this.sendOtpButton = new ButtonField("Send", 12884967424L);
 
     this.submit = new BitmapButtonField(Bitmap.getBitmapResource("submit-button-hover.png"), Bitmap.getBitmapResource("submit-button-p8.png"), "Submit", 12884967424L);
 
     this.helpLabel = new ColoredLabelField(16777215, "Having trouble logging in?", 18014398509481984L)
     {
       protected boolean navigationClick(int status, int time) {
         try {
           OtpView.this.help();
         }
         catch (Exception e) {
         }
         return true;
       }
     };
     this.helpLabel.setFont(Font.getDefault());
   }
 
   private void login()
   {
     if (isValid(this.otpField.getText())) {
       this.controller.login(this.userName, this.sessionToken, this.otpField.getText());
       this.otpField.setText("");
     }
     else {
       diplayError("Please enter the one time password you received", 5000);
     }
   }
 
   private boolean isValid(String oneTimePassword) {
     return !oneTimePassword.equals("");
   }
 
   public boolean onClose()
   {
     this.otpField.setText("");
     this.controller.popScreen(this);
     return true;
   }
}

